<?php

namespace common\component;


use common\models\base\Admin;
use common\models\permis\Task;
use common\utils\CommonUtil;
use yii\web\User;

class WebUser extends User
{
    /**
     * 后台
     */
    const USER_BACKEND = "backend";
    /**
     * 前台
     */
    const USER_FRONTEND = "frontend";


    const USER_MOBILE = "mobile";

    public $userType = self::USER_FRONTEND;
    /**
     * 登录持续的时间
     * @var
     */
    public $loginDuration;


    /**
     * 执行后台登录后的操作
     * @param $identity
     * @param $cookieBased
     * @param $duration
     */
    protected function afterLogin($identity, $cookieBased, $duration)
    {
        switch ($this->userType) {
            case self::USER_BACKEND:
                $this->runBackendAfterlogin($identity,$cookieBased,$duration);
                break;
            case self::USER_FRONTEND:
                $this->runFrontendAfterLogin($identity,$cookieBased,$duration);
                break;
            case self::USER_MOBILE:
                $this->runMobileAfterLogin($identity,$cookieBased,$duration);
                break;
        }
        parent::afterLogin($identity, $cookieBased, $duration);
    }
    private function runBackendAfterlogin($identity,$cookieBased,$duration) {
        //更新登录
        if ($identity instanceof Admin) {
            $identity->login_times++;
            $identity->last_login_time = time();
            $identity->updated_at = time();
            $identity->save();
        }
    }

    /**
     * 前台用户登录之后
     * @param $identity
     * @param $cookieBased
     * @param $duration
     */
    private function runFrontendAfterLogin($identity,$cookieBased,$duration) {
        $attributes = $identity->getAttributes();
        if (count($attributes)) {
            foreach ($attributes as $key=>$val) {
                if ("auth_key"==$key || "password_hash"==$key || "updated_at" == $key) continue;
                $this->setState($key,$val);
            }
        }
        $identity->updateCounters(["login_times"=>1]);
        //绑定登录后事件
        return;
    }

    /**
     * 前台用户登录之后
     * @param $identity
     * @param $cookieBased
     * @param $duration
     */
    private function runMobileAfterLogin($identity,$cookieBased,$duration) {
        $attributes = $identity->getAttributes();
        if (count($attributes)) {
            foreach ($attributes as $key=>$val) {
                if ("auth_key"==$key || "password_hash"==$key || "updated_at" == $key) continue;
                $this->setState($key,$val);
            }
        }
        $identity->updateCounters(["login_times"=>1]);
        //绑定登录后事件
        return;
    }


    public function getState($key) {
        $value = \Yii::$app->getSession()->get($key);
        return is_null($value) ? "" : $value;
    }

    public function setState($key,$value) {
        return \Yii::$app->getSession()->set($key,$value);
    }


    public function beforeLogout($identity)
    {
        if (!parent::beforeLogout($identity)) {
            return false;
        }
        switch ($this->userType) {
            case self::USER_BACKEND:
                $this->runBackendBeforeLogout($identity);
                break;
            case self::USER_FRONTEND:
                $this->runFrontendBeforeLogout($identity->getId());
                break;
        }
        return true;
    }

    /**
     * 清除缓存
     * @param $user_id
     */
    private function runFrontendBeforeLogout($user_id) {
        \common\models\member\User::clearIdentity($user_id);
    }

    /**
     * 后台退出之后
     * @param $identity
     * @return bool
     */
    private function runBackendBeforeLogout($identity) {
        //清除用户权限缓存
        $roleIds = $this->getState("roleIds");
        $keys = [
            Task::CACHE_KEY_ROLE_TASK.":roleIds:".$roleIds,
            "menu:init:role:roleIds:".$roleIds
        ];
        return true;
    }


}